const { extend } = require("jquery");

define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'game/gameorder/index' + location.search,
                    audit_url: 'game/gameorder/audit',
                    del_url: 'game/gameorder/del',
                    multi_url: 'game/gameorder/multi',
                    import_url: 'game/gameorder/import',
                    table: 'gameorder',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id') },
                        { field: 'vipid', title: __('Vipid'), operate: 'LIKE', table: table, class: 'autocontent' },
                        { field: 'gameuserid', title: __('Gameuserid'), operate: 'LIKE', table: table, class: 'autocontent' },
                        { field: 'total', title: __('Total'), operate: 'LIKE', table: table, class: 'autocontent' },
                        { field: 'wxcode', title: __('Wxcode'), operate: 'LIKE', table: table, class: 'autocontent' },
                        { field: 'createtime_text', title: __('CreateTime'), operate: 'LIKE', table: table, class: 'autocontent' },
                        { field: 'auto', title: __('Auto'), operate: 'LIKE', table: table, class: 'autocontent' },
                        { field: 'outcome', title: __('Outcome'), operate: 'LIKE', table: table, class: 'autocontent' },
                        {
                            field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate
                            , buttons:
                                [
                                    {
                                        text: __('Audit'),
                                        classname: 'btn btn-xs btn-success btn-audit btn-dialog',
                                        icon: 'fa fa-check',
                                        url: $.fn.bootstrapTable.defaults.extend.audit_url,
                                        refresh: true,//刷新当前页面
                                        visible: function (row) {
                                            return row.auto == 0;
                                        }
                                    }
                                ]

                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            // 添加事件，点击头部审核会打开弹窗
            $(".btn-audit").on("click", function () {
                //获取选中的id
                let ids = Table.api.selectedids(table);

                // 循环遍历打开每一个弹窗
                ids.forEach(function (id) {
                    Fast.api.open(
                        $.fn.bootstrapTable.defaults.extend.audit_url + '?ids=' + id,
                    );
                })

            })
        },
        audit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
